﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

namespace LocationAwareness
{
    public partial class SettingsGUI : Form
    {
        /// <summary>
        /// List of filter checkboxes
        /// </summary>
        private List<CheckBox> defaultFilter;

        /// <summary>
        /// Default constructor
        /// </summary>
        public SettingsGUI(Settings s)
        {
            InitializeComponent();

            defaultFilter = new List<CheckBox>();
            defaultFilter.Add(checkBoxBar);
            defaultFilter.Add(checkBoxCinema);
            defaultFilter.Add(checkBoxClub);
            defaultFilter.Add(checkBoxCulture);
            defaultFilter.Add(checkBoxFastFood);
            defaultFilter.Add(checkBoxLocalTraf);
            defaultFilter.Add(checkBoxOther);
            defaultFilter.Add(checkBoxRestaurant);
            defaultFilter.Add(checkBoxSuperm);

            catBoxChangeServer.TextOnPicture = LocawaToken.TkChange;
            settings = s;
        }

        /// <summary>
        /// Internal represantation of settings
        /// </summary>
        private Settings settings;

        /// <summary>
        /// Actual settings represented in SettingsGUI
        /// </summary>
        public Settings Settings
        {
            get
            {
                return settings;
            }
        }

        /// <summary>
        /// Loads passed settings in GUI
        /// </summary>
        private void SettingsGUI_Load(object sender, EventArgs e)
        {
            if (settings != null)
            {
                if (settings.Update > 0)
                {
                    radioButtonAutomatic.Checked = true;
                    txtBoxSec.Text = settings.Update.ToString();
                }
                else
                    radioButtonManual.Checked = true;

                radioButtonShow.Checked = settings.ShowButtons;
                radioButtonShowNot.Checked = !settings.ShowButtons;
                txtBoxServerAddress.Text = settings.ServiceUrl;
                cbMaxDistance.Text = settings.DistanceToPoi;

                radioButtonAll.Checked = true;
                if (settings.DefaultFilter.Count > 0)
                {
                    for (int i = 0; i < defaultFilter.Count; i++)
                    {
                        if (settings.DefaultFilter.ContainsKey(defaultFilter[i].Name))
                        {
                            defaultFilter[i].Checked = settings.DefaultFilter[defaultFilter[i].Name];
                            if (defaultFilter[i].Checked)
                                radioButtonPersonal.Checked = true;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Closes SettingsGUI without saving
        /// </summary>
        private void menuItemCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        /// <summary>
        /// Closes SettingsGUI with saving
        /// </summary>
        private void menuItemSave_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        /// <summary>
        /// User input handling.
        /// </summary>
        private void btnChangeServer_Click(object sender, EventArgs e)
        {
            txtBoxServerAddress.Enabled = true;
            txtBoxServerAddress.Focus();
        }

        /// <summary>
        /// User input handling.
        /// </summary>
        private void txtBoxServerAddress_LostFocus(object sender, EventArgs e)
        {
            txtBoxServerAddress.Enabled = false;
        }

        /// <summary>
        /// User input handling.
        /// </summary>
        private void radioButtonPersonal_CheckedChanged(object sender, EventArgs e)
        {
            checkBoxBar.Enabled = radioButtonPersonal.Checked;
            checkBoxCinema.Enabled = radioButtonPersonal.Checked;
            checkBoxClub.Enabled = radioButtonPersonal.Checked;
            checkBoxCulture.Enabled = radioButtonPersonal.Checked;
            checkBoxFastFood.Enabled = radioButtonPersonal.Checked;
            checkBoxLocalTraf.Enabled = radioButtonPersonal.Checked;
            checkBoxRestaurant.Enabled = radioButtonPersonal.Checked;
            checkBoxSuperm.Enabled = radioButtonPersonal.Checked;
            checkBoxOther.Enabled = radioButtonPersonal.Checked;

            if (!radioButtonPersonal.Checked)
            {
                checkBoxBar.Checked = false;
                checkBoxCinema.Checked = false;
                checkBoxClub.Checked = false;
                checkBoxCulture.Checked = false;
                checkBoxFastFood.Checked = false;
                checkBoxLocalTraf.Checked = false;
                checkBoxRestaurant.Checked = false;
                checkBoxSuperm.Checked = false;
                checkBoxOther.Checked = false;
            }
        }

        /// <summary>
        /// If SettingsGUI closed with saving. 
        /// Method saves choosen settings in SettingsGUI.Settings 
        /// </summary>
        private void SettingsGUI_Closing(object sender, CancelEventArgs e)
        {
            if (this.DialogResult == DialogResult.OK)
            {
                int updateSec = Convert.ToInt32(txtBoxSec.Text);
                if (radioButtonAutomatic.Checked && updateSec > 0)
                    settings.Update = (uint)updateSec;
                else
                    settings.Update = 0;

                settings.ShowButtons = radioButtonShow.Checked;
                settings.ServiceUrl = txtBoxServerAddress.Text;
                settings.DistanceToPoi = cbMaxDistance.Text;

                if (radioButtonPersonal.Checked)
                {
                    for (int i = 0; i < defaultFilter.Count; i++)
                    {
                        if (defaultFilter[i].Checked)
                            settings.DefaultFilter.Add(defaultFilter[i].Name, defaultFilter[i].Checked);
                    }
                }
            }
        }
    }
}